__author__ = 'zoulida'
import pymongo
import datetime
from sdufe.everydayRun.saveTickTDX import QA_SU_save_stock_transactionByOneDay
from sdufe.tools.LogCut import *
logger = Logger(logName='mongodbFetch.out', logLevel="DEBUG", logger="quantaxit").getlog()

def manageStatDays():
    today = datetime.date.today()
    yestoday = today + datetime.timedelta(days=-1)
    z30daysago = yestoday + datetime.timedelta(days=-15)  # 更改为两月了
    # dates = get_date_list(datetime.date(2018, 6, 30), datetime.date(2018, 7, 16))
    dates = get_date_list(z30daysago, yestoday)
    return dates

# 获取从起始日期到截止日期中间的的所有日期，前后都是封闭区间
def get_date_list(begin_date, end_date):
    date_list = []
    while begin_date <= end_date:
        # date_str = str(begin_date)
        date_list.append(begin_date)
        begin_date += datetime.timedelta(days=1)
    return date_list

def saveTickTDXOneDay():
    monitorcol = getCollectionMonitor()
    #count = tdxcol.find_one({'date': strday})
    #maxdaystr = tdxcol.find().sort({'date': -1}).skip(0).limit(1);
    daylist = manageStatDays()


    for date in daylist:
        str_date = str(date)
        myquery = {"date": "%s" % str_date}
        #x = mycol.find_one(myquery)
        x = monitorcol.find_one(myquery)
        if x is None:
            continue
        print(x)

        if x['isopen'] is True and x['tdx'] is False :
            #print("ToDo")
            try:
                QA_SU_save_stock_transactionByOneDay(dateStr=str_date)
            except Exception as e:
                import traceback
                print('traceback.print_exc():', traceback.print_exc())
                logger.info(e)



    #print(maxdaystr)


def getTDXCollection():
    myclient = pymongo.MongoClient("mongodb://192.168.1.104:27017/")
    mydb = myclient["quantaxis"]
    mycol = mydb["stock_transaction"]
    return mycol

def getCollectionMonitor():
    myclient = pymongo.MongoClient("mongodb://192.168.1.104:27017/")
    mydb = myclient["monitor"]
    monitorcol = mydb["updateDate"]
    return monitorcol

def main():
    saveTickTDXOneDay()

if __name__ == '__main__':
    saveTickTDXOneDay()